<?php
class AuthAction extends BaseApiAction {
	private $shop = false;
	private $tplkey = "";
	private $token;
	// 上网的机器的物理地址
	private $mac;
	// 上网的IP地址
	private $ip;
	// 上网的网关
	private $gw_id;
	
	private function isonline($val) {
		// 引用时间格式化工具类
		import ( "ORG.Util.Date" );
		$dt = new Date ( time () );
		$left = $dt->dateDiff ( $val, 's' ); //默认7天试用期
		return $left;
	
	}
	
	
	public function index() {
		// 获取上网IP地址
		if (! empty ( $_REQUEST ['ip'] )) {
			$this->ip = $_REQUEST ['ip'];
		
		}
		// 获取上网网关
		if (! empty ( $_REQUEST ['gw_id'] )) {
			$this->gw_id = $_REQUEST ['gw_id'];
		
		}
		// $this->mac = '';
		// 上网机器的mac地址
		if (! empty ( $_REQUEST ['mac'] )) {
			$this->mac = $_REQUEST ['mac'];
		}
		// 上网认证密钥
		if (! empty ( $_REQUEST ['token'] )) {
			$tk = $_REQUEST ['token'];
			$db = new Model ();
			$authdb = D ( 'Authlist' );
			if(!empty($this->mac)){
				//$where ['mac'] = $this->mac;
			}
			
			$where ['token'] = $tk;
			$rs = $authdb->where ( $where )->field ()->find ();
			$mdb=D('Member');
			$wherem['token']=$tk;
			$user=$mdb->where($wherem)->find();
			 	
			 if($user!=false){
			 	// 将当前的机器的物理地址存入到Member表中
				$mdata['mac']=$this->mac;
				$mdb->where($wherem)->save($mdata);
			 }
			if ($rs) {
				//update time 没有限制上网时间
				if (empty ( $rs ['over_time'] ) || $rs ['over_time'] == "") {
					//no limit
					$this->token = $tk;
					echo ("Auth: 1n");
					echo ("Messages: Allow Accessn");
					$data ['mac'] = $this->mac;
					// 登录的IP地址
					$data ['login_ip'] = $this->ip;
					// 连接次数
					$data ['pingcount'] = $rs ['pingcount'] + 1;
					// 最后登录时间
					$data ['last_time'] = time (); //
					// 更新时间
					$data ['update_time'] = time (); //
					// 向Authlist表中添加数据
					$authdb->where ( $where )->save ( $data );
					exit ();
				} else {
					//limit 存在上网限制时间
					$lf = $rs ['over_time'] - time ();
					if ($lf < 0) {
						// 上网超时
						echo ("Auth: 0n");
						echo ("Messages: No Accessn");
						exit ();
					} else {
						// 上网没超时
						$this->token = $tk;
						echo ("Auth: 1n");
						echo ("Messages: Allow Accessn");
						// 上网的机器的物理地址
						$data ['mac'] = $this->mac;
						// 上网的ip
						$data ['login_ip'] = $this->ip;
						// 上网的连接次数
						$data ['pingcount'] = $rs ['pingcount'] + 1;
						// 最后一次上网时间
						$data ['last_time'] = time (); //
						// 上网的更新时间
						$data ['update_time'] = time (); //
						// 保存数据
						$authdb->where ( $where )->save ( $data );
						exit ();
					}
				}
			}else {
				echo ("Auth: 0n");
				echo ("Messages: No Accessn");
				exit ();
			}
		} else {
			echo ("Auth: 0n");
			echo ("Messages: No Accessn");
			exit ();
		}
	}

}